// pages/pay/index.js

import config from '../../etc/config'
const {
  $Toast
} = require('../../components/dist/base/index');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        coupon: {
            name: '',
            money: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      wx.hideShareMenu()
        this.data.orderId = options.orderId
        this.getorderDetail(options.orderId)
    },
    getorderDetail(orderId) {
        let params = {
            orderId: orderId
        }
        let token = wx.getStorageSync('token')
        config._post('/order/detail', token, params).then(res => {
            if (res.data) {
                this.setData({
                    detailsList: res.data.order,
                    shopName: res.data.shopName
                })
            }
        })
    },


    goCoupon() {
        wx.navigateTo({
            url: '../my-coupon/index?orderId=' + this.data.orderId,
        })
    },

    pay() {
      let that = this
        let params = {
            orderId: this.data.orderId
        }
        if (this.data.coupon) {
            params.couponId = this.data.coupon.id
        }
        let token = wx.getStorageSync('token')
        config._post('/pay/wxPay', token, params).then(res => {
          console.log(res.data)
            if (res.code == 0 && res.data) {
                wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: 'MD5',
                    paySign: res.data.paySign,
                    success(res) {
                        $Toast({
                            content: '支付成功'
                        });
                      var pages = getCurrentPages(); // 获取页面栈
                      var prevPage = pages[pages.length - 2]; // 上一个页面
                      if (that.data.detailsList.type == 5){
                        prevPage.setData({
                          current: '1'
                        })
                        wx.navigateBack()
                      }else{
                        prevPage.setData({
                          current: '2'
                        })
                        wx.navigateBack()
                      }
                    },
                    fail(res) {
                        $Toast({
                            content: '支付失败'
                        });
                    }
                })
            } else {
                $Toast({
                    content: '支付失败'
                });
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})